<?php

namespace app\admin\controller;

use app\common\controller\AdminController;
use chrome\ChromePhp;
use common\Common;
use uploadImg\UtilImage;
use app\common\model\blfyy\LibStatistic;
use UtilDate\UtilDate;

/**
 * 统计
 * @name Statistic.php
 * @package blfyy
 * @category controller
 * @link http://www.chanekeji.com
 * @author syben
 * @version 1.0
 * @copyright CHANGE INC
 * @since 2021-03-11
 */
class Statistic extends AdminController
{
    private $lib_statistic;

    /**
     * 构造函数
     */
    function __construct()
    {
        parent::__construct();
        $this->lib_statistic = new LibStatistic();
    }

    /**
     * 统计列表页面
     **/
    function statisticList()
    {
        $this->getSetMenu($this);
        $result = $this->lib_statistic->getKeyIndexOfSale();
        $this->assign('keyIndex', $result['data']);
        //日志记录
        $this->log(__CLASS__, __FUNCTION__, "统计列表页面", 1, 'view');
        return $this->fetch("Statistic/orderStatisticList/orderStatisticList");
    }

    /**
     * 删除统计
     */
    function deleteStatictic()
    {
        $id = input('ids');
        $result = $this->lib_statistic->deleteForm($id);
        $this->log(__CLASS__, __FUNCTION__, "删除统计", 0, 'del');
        echo json_encode($result);
    }

    /**
     * 统计数量
     */
    function getStatisticData()
    {
        $start_time = input('start_time');
        $end_time = input('end_time');
        $conditions = array();
        if ($start_time != '' && $end_time != '') {
            array_push($conditions, array('field' => 'add_time', 'operator' => ">=", 'value' => $start_time));
            array_push($conditions, array('field' => 'add_time', 'operator' => "<=", 'value' => $end_time));
        }
        $result = $this->lib_statistic->getAllStatistic($conditions);
        $this->log(__CLASS__, __FUNCTION__, "统计数量", 0, 'getStatisticData');
        echo json_encode($result);
    }


    /**
     * 统计分页
     */
    function pagingStatistic()
    {
        $page = $this->getPageInfo($this);
        $sort = 'add_time desc';
        $conditionList = $this->getPagingList($this, array());
        array_push($conditionList, array('field' => 'type', 'operator' => "=", 'value' => input('type')));
        $result = $this->lib_statistic->pagingStatictic($page, $conditionList, $sort);
        $this->log(__CLASS__, __FUNCTION__, "统计分页", 0, 'view');
        echo json_encode($result);
    }

    /**
     * 按日期获取不同状态销售额变化数据
     */
    function getQuantityData()
    {
        $dayCount = (int)input('dayCount');
        if (null == $dayCount || '' == $dayCount) {
            $dayCount = 1;
        }
        $utilDate = new UtilDate();
        if ($dayCount > 0) {
            $dateList = $utilDate->getSerialDate($dayCount, 'asc');
        } else {
            $dayCount = 1;
            $dateList = $utilDate->getSerialDate($dayCount, 'asc', 0);
        }
        for ($i = 0; $i < $dayCount; $i++) {
            $time = date('Y-m-d', strtotime($dateList[$i]));
            $startDate = $time . ' 00:00:00';
            $endDate = $time . ' 23:59:59';
            $result = $this->lib_statistic->getQuantityData($startDate, $endDate);
            $dateList[$i] = array(
                'amount' => $result['data'],
                'add_date' => $dateList[$i]
            );
        }
        $result['data'] = $dateList;
        $xList = array();
        $yList[0] = array('name' => '平台交易额', 'data' => array(), "color" => "#7cb5ec");
        foreach ($result['data'] as &$per) {
            $per['amount'] = (int)($per['amount']);
            array_push($xList, $per['add_date']);
            array_push($yList[0]['data'], $per['amount']);
        }
        $result['data'] = array(
            'xData' => $xList,
            'yData' => $yList,
            'title' => '平台交易额统计',
            'subtitle' => '对平台交易额趋势分析',
            'xtitle' => '金额',
            'tooltip' => '元',
        );
        echo json_encode($result);
    }

    /**
     * 按日期获取不同状态销售额变化数据
     */
    function getQuantityData2()
    {
        $dayCount = (int)input('dayCount');
        if (null == $dayCount || '' == $dayCount) {
            $dayCount = 1;
        }
        $utilDate = new UtilDate();
        if ($dayCount > 0) {
            $dateList = $utilDate->getSerialDate($dayCount, 'asc');
        } else {
            $dayCount = 1;
            $dateList = $utilDate->getSerialDate($dayCount, 'asc', 0);
        }
        for ($i = 0; $i < $dayCount; $i++) {
            $time = date('Y-m-d', strtotime($dateList[$i]));
            $startDate = $time . ' 00:00:00';
            $endDate = $time . ' 23:59:59';
            $result = $this->lib_statistic->getQuantityData2($startDate, $endDate);
            $dateList[$i] = array(
                'amount' => $result['data'],
                'add_date' => $dateList[$i]
            );
        }
        $result['data'] = $dateList;
        $xList = array();
        $yList[0] = array('name' => '访客数量', 'data' => array(), "color" => "#7cb5ec");
        foreach ($result['data'] as &$per) {
            $per['amount'] = (int)($per['amount']);
            array_push($xList, $per['add_date']);
            array_push($yList[0]['data'], $per['amount']);
        }
        $result['data'] = array(
            'xData' => $xList,
            'yData' => $yList,
            'title' => '平台访客数量统计',
            'subtitle' => '对访客数量趋势分析',
            'xtitle' => '数量',
            'tooltip' => '个',
        );
        echo json_encode($result);
    }
}